package com.zhouys.servlet;

import com.zhouys.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class SessionDemo1  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();

//        session.setAttribute("name","测试");
        session.setAttribute("name",new Person("测试",001));
//        获取sessionID
        String sessionID = session.getId();
        //判断session是不是新创建的
        if (session.isNew()){
            resp.getWriter().write("session创建成功，ID："+sessionID);
        }else {
            resp.getWriter().write("session创建成功,ID:"+sessionID);
        }




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
